package com.maidada.codesharebackend.controller;

import cn.dev33.satoken.annotation.SaCheckRole;
import cn.dev33.satoken.annotation.SaIgnore;
import cn.dev33.satoken.stp.StpUtil;
import com.maidada.codesharebackend.common.BaseRequest;
import com.maidada.codesharebackend.common.CommonResult;
import com.maidada.codesharebackend.common.PageResult;
import com.maidada.codesharebackend.constant.UserRoleConstant;
import com.maidada.codesharebackend.model.dto.user.UserAddRequest;
import com.maidada.codesharebackend.model.dto.user.UserLoginRequest;
import com.maidada.codesharebackend.model.dto.user.UserQueryRequest;
import com.maidada.codesharebackend.model.dto.user.UserUpdateRequest;
import com.maidada.codesharebackend.model.vo.UserVO;
import com.maidada.codesharebackend.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

/**
 * 用户表 Controller
 *
 * @author wulinxuan
 */
@Slf4j
@RestController
@RequestMapping("/user")
public class UserController {

    @Resource
    private UserService userService;

    /**
     * 用户登录
     *
     * @param userLoginRequest 登录请求
     * @return 用户信息
     */
    @PostMapping("/login")
    public CommonResult<UserVO> login(@Valid @RequestBody UserLoginRequest userLoginRequest) {
        UserVO userVO = userService.login(userLoginRequest);
        return CommonResult.success(userVO);
    }

    /**
     * 用户登出
     *
     * @return 是否成功
     */
    @PostMapping("/logout")
    public CommonResult<Boolean> logout() {
        Boolean result = userService.logout();
        return CommonResult.success(result);
    }

    /**
     * 获取当前用户信息
     *
     * @return 用户信息
     */
    @GetMapping("/getCurrentUser")
    public CommonResult<UserVO> getCurrentUser() {
        // 检查是否登录
        if (!StpUtil.isLogin()) {
            return CommonResult.error(40100, "未登录");
        }
        // 从 Sa-Token session 中获取当前登录用户 ID
        long userId = StpUtil.getLoginIdAsLong();
        UserVO userVO = userService.getCurrentUser(userId);
        return CommonResult.success(userVO);
    }

    /**
     * 新增用户
     *
     * @param userAddRequest 新增请求
     * @return 用户id
     */
    @SaCheckRole(UserRoleConstant.ADMIN)
    @PostMapping("/add")
    public CommonResult<Long> addUser(@Valid @RequestBody UserAddRequest userAddRequest) {
        Long userId = userService.addUser(userAddRequest);
        return CommonResult.success(userId);
    }

    /**
     * 删除用户
     *
     * @param baseRequest 请求参数
     * @return 是否成功
     */
    @SaCheckRole(UserRoleConstant.ADMIN)
    @PostMapping("/delete")
    public CommonResult<Boolean> deleteUser(@Valid @RequestBody BaseRequest baseRequest) {
        Boolean result = userService.deleteUser(baseRequest.getId());
        return CommonResult.success(result);
    }

    /**
     * 修改用户
     *
     * @param userUpdateRequest 修改请求
     * @return 是否成功
     */
    @SaCheckRole(UserRoleConstant.ADMIN)
    @PostMapping("/update")
    public CommonResult<Boolean> updateUser(@Valid @RequestBody UserUpdateRequest userUpdateRequest) {
        Boolean result = userService.updateUser(userUpdateRequest);
        return CommonResult.success(result);
    }

    /**
     * 分页查询用户
     *
     * @param userQueryRequest 查询请求
     * @return 分页结果
     */
    @SaCheckRole(UserRoleConstant.ADMIN)
    @GetMapping("/page")
    public CommonResult<PageResult<UserVO>> pageUser(@Valid UserQueryRequest userQueryRequest) {
        PageResult<UserVO> pageResult = userService.pageUser(userQueryRequest);
        return CommonResult.success(pageResult);
    }

    /**
     * 获取用户详情
     *
     * @param id 用户id
     * @return 用户信息
     */
    @SaCheckRole(UserRoleConstant.ADMIN)
    @GetMapping("/get")
    public CommonResult<UserVO> getUserById(@RequestParam Long id) {
        UserVO userVO = userService.getUserById(id);
        return CommonResult.success(userVO);
    }
}

